﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections;
using System.Windows.Markup;

namespace SilverTile.Xaml
{
    [ContentProperty("BackgroundSprites")]
    public class GameAreaGrid : FrameworkElement
    {

        public GameAreaGrid()
        {
            BackgroundSprites = new List<SpriteTile>();
        }

        public static readonly DependencyProperty BackgroundSpritesProperty = DependencyProperty.RegisterAttached("BackgroundSprites", typeof(List<SpriteTile>), typeof(GameAreaGrid), null);
        public List<SpriteTile> BackgroundSprites
        {
            get { return (List<SpriteTile>)GetValue(BackgroundSpritesProperty); }
            set { SetValue(BackgroundSpritesProperty, value); }
        }

        public static readonly DependencyProperty ColumnsProperty = DependencyProperty.RegisterAttached("Columns", typeof(Int32), typeof(GameAreaGrid), null);
        public Int32 Columns
        {
            get { return (Int32)GetValue(ColumnsProperty); }
            set { SetValue(ColumnsProperty, value); }
        }

        public static readonly DependencyProperty RowsProperty = DependencyProperty.RegisterAttached("Rows", typeof(Int32), typeof(GameAreaGrid), null);
        public Int32 Rows
        {
            get { return (Int32)GetValue(RowsProperty); }
            set { SetValue(RowsProperty, value); }
        }

        public static readonly DependencyProperty TileWidthProperty = DependencyProperty.RegisterAttached("TileWidth", typeof(Int32), typeof(GameAreaGrid), null);
        public Int32 TileWidth
        {
            get { return (Int32)GetValue(TileWidthProperty); }
            set { SetValue(TileWidthProperty, value); }
        }

        public static readonly DependencyProperty TileHeightProperty = DependencyProperty.RegisterAttached("TileHeight", typeof(Int32), typeof(GameAreaGrid), null);
        public Int32 TileHeight
        {
            get { return (Int32)GetValue(TileHeightProperty); }
            set { SetValue(TileHeightProperty, value); }
        }

        public static readonly DependencyProperty AreaNameProperty = DependencyProperty.Register("AreaName", typeof(string), typeof(GameAreaGrid), null);
        public string AreaName
        {
            get { return (string)GetValue(AreaNameProperty); }
            set { SetValue(AreaNameProperty, value); }
        }

        public Boolean Validate()
        {

            bool[,] gridCells = new bool[Columns, Rows];
            int tileCount = 0;

            foreach (SpriteTile tile in BackgroundSprites)
            {
                if (gridCells[tile.X, tile.Y] == true)
                {
                    return false; // Cell is already occupied
                }

                // Mark cell as occupied
                gridCells[tile.X, tile.Y] = true;
                tileCount++;

            }

            // Check the number of tiles placed. If it matches the number of tiles expected
            // then we have one in each grid cell since we have no overlaps
            if (!tileCount.Equals(Columns * Rows))
            {
                return false;
            }

            return true;

        }

    }
}
